* THIS DO-FILE GENERATE VARIABLES FOR MENTAL HEALTH DIAGNOSES USING LPSYADM AND LPYSDIAG

global raw "Path to rawdata"

use panelraw_v7, clear
keep pnr age birthyear year
gen post=birthyear>1956
drop birthyear
sort pnr age
save tempdiag, replace

use $raw/lpsyadm2016.dta, clear
merge m:m recnum using $raw/lpsydiag2016.dta, nogen norep keep(mat)
	ren V_ALDER age
	sort pnr age
	gen d0=regexs(0) if (regexm(C_DIAG, "^[D][A-Z]"))
	gen d1=regexs(0) if (regexm(C_DIAG, "^[D][A-Z][0-9]"))
	gen d2=regexs(0) if (regexm(C_DIAG, "^[D][A-Z][0-9][0-9]"))
	gen d3=regexs(0) if (regexm(C_DIAG, "^[D][A-Z][0-9][0-9][0-9]"))
	replace d3=d2+"0" if d3==""
	gen ad0=regexs(0) if (regexm(C_ADIAG, "^[D][A-Z]"))
	gen ad1=regexs(0) if (regexm(C_ADIAG, "^[D][A-Z][0-9]"))
	gen ad2=regexs(0) if (regexm(C_ADIAG, "^[D][A-Z][0-9][0-9]"))
	gen ad3=regexs(0) if (regexm(C_ADIAG, "^[D][A-Z][0-9][0-9][0-9]"))
	replace ad3=ad2+"0" if ad3==""
	gen td0=regexs(0) if (regexm(C_TILDIAG, "^[D][A-Z]")) & C_TILDIAG!=""
	gen td1=regexs(0) if (regexm(C_TILDIAG, "^[D][A-Z][0-9]")) & C_TILDIAG!=""
	gen td2=regexs(0) if (regexm(C_TILDIAG, "^[D][A-Z][0-9][0-9]")) & C_TILDIAG!=""
	gen td3=regexs(0) if (regexm(C_TILDIAG, "^[D][A-Z][0-9][0-9][0-9]")) & C_TILDIAG!=""
	replace td3=td2+"0" if td3==""

	gen bipolar=d2=="DF31" | ad2=="DF31" | td2=="DF31"
	gen mania=d2=="DF30" | ad2=="DF30" | d2=="DF34" | ad2=="DF34" | td2=="DF30" | td2=="DF34" 
	gen schizo=d1=="DF2" | ad1=="DF2" | td1=="DF2"
	gen depres=td2=="DF32" | td2=="DF33" | td2=="DF34" | td2=="DF35" | td2=="DF36" | td2=="DF37" | td2=="DF38" | td2=="DF39" | d2=="DF32" | ad2=="DF32" | d2=="DF33" | ad2=="DF33" | d2=="DF34" | ad2=="DF34" | d2=="DF35" | ad2=="DF35" | d2=="DF36" | ad2=="DF36" | d2=="DF37" | ad2=="DF37" | d2=="DF38" | ad2=="DF38" | d2=="DF39" | ad2=="DF39"
	gen anxiety=d3=="DF400" | ad3=="DF400" | d3=="DF401" | ad3=="DF401" | d3=="DF409" | ad3=="DF409" | d2=="DF41" | ad2=="DF41"
	gen anxiety_wide=anxiety==1 | d2=="DF42" | ad2=="DF42" | d2=="DF43" | ad2=="DF43"
	
collapse (max) bipolar-anxiety_wide, by(pnr age)

gen bipman=bipolar==1 | mania==1
gen mania_nobip = mania==1 & bipolar==0
gen depres_nobipman = depres==1 & bipolar==0 & mania==0
gen schizo_nobipman = schizo==1 & bipolar==0 & mania==0

	label var bipolar "Bipolar"
	label var bipman "Bipolar or mania"
	label var mania "Mania"
	label var mania_nobip "Mania"
	label var schizo "Schizophrenia"
	label var schizo_nobipman "Schizophrenia"
	label var depres "Depression"
	label var depres_nobipman "Depression"
	label var bipolar "Bipolar"
	label var anxiety "Anxiety"
	label var anxiety_wide "Anxiety, incl. OCD and PTSD"

order _all, alphabetic
order pnr age, first
merge 1:1 pnr age using tempdiag, norep nogen

mvencode bipolar* bipman* depres* mania* schizo* anxiety*, mv(0) o
	foreach x of varlist anxiety-schizo_nobipman {
		di "`: var label `x''"
		bys pnr (year): egen `x'_ev=max( `x' )
		label var `x'_ev "`: var label `x'' (ever)"
		gen age_`x'=age if `x'==1
		bys pnr (year): egen age_first_`x'=min(age_`x')
		gen year_to_`x'=age-age_first_`x'
		label var age_first_`x' "Age first diagnosis (`: var label `x'')" 
		label var year_to_`x' "Year to diagnosis (`: var label `x'')" 
		drop age_`x'
		label var `x' "`: var label `x'' (year)" 
	}
	foreach x of varlist bipman_ev bipolar_ev mania_nobip_ev {
		gen `x'_post=post* `x'
		label var `x'_post "`: var label `x'' x post" 
	}
drop post
order _all, alphabetic
order pnr age, first
order age_*, last
sort pnr age
save diagnoses_v6, replace
